package edu.stanford.hardon.Tabs;

import java.io.File;
import java.util.Date;

import edu.stanford.hardon.DatabaseHelper;
import edu.stanford.hardon.R;
import edu.stanford.hardon.TravelCore;
import edu.stanford.hardon.TravelHard;
import edu.stanford.hardon.TravelHardConstants;
import edu.stanford.hardon.Media.Media;
import edu.stanford.hardon.Media.MediaType;
import edu.stanford.hardon.Media.RichMedia;
import edu.stanford.hardon.Media.TextMedia;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ManageTab extends GenericTab {

	private ListView lv;
	private MediaTabState state = MediaTabState.MAIN;
	private RelativeLayout mainView;
	private RelativeLayout photoView;
	private ListView lva;
	private RelativeLayout audioView;
	private RelativeLayout noteView;
	private RelativeLayout videoView;
	private Media currentMedia;
	
	public ManageTab(int Id, String Text, int iconResourceId, LinearLayout tabLayout, TravelHard th) {
		super(Id, Text, iconResourceId, tabLayout, th);
		lv = (ListView) th.findViewById(R.id.mList);
		lv.setAdapter(new ManageListAdapter(th, this));
		state = MediaTabState.MAIN;
		currentMedia = null;
		mainView = (RelativeLayout) th.findViewById(R.id.mListViewMain);
		photoView = (RelativeLayout)th.findViewById(R.id.mListViewPhoto);
		EditText et = (EditText) photoView.findViewById(R.id.mListPhotoTextAnn);
		et.setVisibility(View.GONE);
		lva = (ListView) th.findViewById(R.id.mListOfAnnotations);
		lva.setAdapter(new ManageAnnotationListAdapter(th,this));
		lva.setVisibility(View.GONE);
	}

	@Override
	public void startTab() {
		updateState();
	}
	
	private void updateState(){
		switch(state){
		case MAIN:{
			mainView.setVisibility(View.VISIBLE);
			photoView.setVisibility(View.GONE);
//			videoView.setVisibility(View.GONE);
//			audioView.setVisibility(View.GONE);
//			noteView.setVisibility(View.GONE);
			// TODO Auto-generated method stub
			DatabaseHelper dbh = TravelCore.getDBH();
			dbh.open();
			((ManageListAdapter) lv.getAdapter()).setItems(dbh.getNonAnnotations());
			dbh.close();
			((BaseAdapter)lv.getAdapter()).notifyDataSetChanged();
			break;
		}
		case PHOTO_VIEW:{
			mainView.setVisibility(View.GONE);
			photoView.setVisibility(View.VISIBLE);
			ImageView iv = (ImageView) photoView.findViewById(R.id.mListPhotoHolder);
			File photo = new File(((RichMedia)currentMedia).getPath());
			Log.i("PHOTO VIEW","trying to set photo "  + photo.toString());
			if(photo.exists()){
//				iv.setImageURI(Uri.parse(photo.toString()));
				BitmapFactory.Options opts = new BitmapFactory.Options();
				Bitmap b = BitmapFactory.decodeFile(photo.toString(), opts);
				iv.setImageBitmap(Bitmap.createScaledBitmap(b, 480, 320, true));
				b.recycle();
			}
			DatabaseHelper dbh = TravelCore.getDBH();
			dbh.open();
			((ManageAnnotationListAdapter) lva.getAdapter()).setItems(dbh.getAnnotations(currentMedia.getDBID()));
			dbh.close();
			if(lva.getAdapter().getCount() > 0){
				lva.setVisibility(View.VISIBLE);
			}
			else{
				lva.setVisibility(View.GONE);
			}
			((BaseAdapter)lva.getAdapter()).notifyDataSetChanged();

			Button b = (Button) photoView.findViewById(R.id.mListPhotoAddText);
			b.setOnClickListener(new OnClickListener(){
				
				public void onClick(View v) {
					if(((Button)v).getText().equals("Add Note") || ((Button)v).getText().equals("Edit Note")){
						((Button)v).setText("Save Note");
						EditText et = (EditText) photoView.findViewById(R.id.mListPhotoTextAnn);
						et.setText("");
						et.setVisibility(View.VISIBLE);
					}
					else{
						((Button)v).setText("Add Note");
						
						//save out the annotation here
						EditText et = (EditText) photoView.findViewById(R.id.mListPhotoTextAnn);
						String noteText = et.getText().toString();
						if(!noteText.equals("")){
							//final String date = getTimeNow();
							Date date = new Date();
							Double lat = 0.0, lng = 0.0, alt = 0.0;
							if(th.location != null) {
								lat = th.location.getLatitude();
								lng = th.location.getLongitude();
								alt = th.location.getAltitude();
							}	
							TextMedia textMedia = new TextMedia(null, TravelHardConstants.TRIP_ID, MediaType.NOTE, lat, lng, alt, date, null, currentMedia.getDBID(), noteText);
							((ManageAnnotationListAdapter) lva.getAdapter()).addItem(textMedia);
							lva.setVisibility(View.VISIBLE);
							DatabaseHelper dbhh = TravelCore.getDBH();
							dbhh.open();
							dbhh.insert(textMedia);
							dbhh.close();
							((BaseAdapter)lva.getAdapter()).notifyDataSetChanged();
						}
						
						
						
						et.setVisibility(View.GONE);
						
					}
					
				}
				
			});
//			videoView.setVisibility(View.GONE);
//			audioView.setVisibility(View.GONE);
//			noteView.setVisibility(View.GONE);
			break;
		}
		case AUDIO_VIEW:{
			
			break;
		}
		case NOTE_VIEW:{
			
			break;
		}
		case VIDEO_VIEW:{
			
			break;
		}
		default:{
			mainView.setVisibility(View.VISIBLE);
			photoView.setVisibility(View.GONE);
			videoView.setVisibility(View.GONE);
			audioView.setVisibility(View.GONE);
			noteView.setVisibility(View.GONE);
			// TODO Auto-generated method stub
			DatabaseHelper dbh = TravelCore.getDBH();
			dbh.open();
			((ManageListAdapter) lv.getAdapter()).setItems(dbh.getNonAnnotations());
			dbh.close();
			((BaseAdapter)lv.getAdapter()).notifyDataSetChanged();
			break;
		}
		}
		
	}
	
	public void switchState(MediaTabState newState, Media m){
		state = newState;
		currentMedia = m;
		updateState();
	}

	@Override
	public void stopTab() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setPaused() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setResumed() {
		// TODO Auto-generated method stub
		
	}
	
	public boolean onBackButtonDown(){
		switch(state){
		case MAIN:{
			return false;
		}
		case PHOTO_VIEW:{
			state = MediaTabState.MAIN;
			ImageView iv = (ImageView) photoView.findViewById(R.id.mListPhotoHolder);
			iv.setImageBitmap(null);
			System.gc();
			updateState();
			return true;
			}
		case AUDIO_VIEW:{
			state = MediaTabState.MAIN;
			updateState();
			return true;
		}
		case NOTE_VIEW:{
			state = MediaTabState.MAIN;
			updateState();
			return true;
		}
		case VIDEO_VIEW:{
			state = MediaTabState.MAIN;
			updateState();
			return true;
		}
		default:{
				return false;
		}
		}
	}

}
